package com.weiyun.notebook

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.weiyun.notebook.Entity.Note

class MyAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public var TYPE_LINEAR_LAYOUT = 0
    public var TYPE_GRID_LAYOUT = 1
    private var viewType = 0
    //拿到对应数据
    var mBeanList: List<Note>
    var mLayoutInflater: LayoutInflater
    var mContext: Context
    var noteViewModel: NoteViewModel
   constructor( context:Context,mBeanList: List<Note>,noteViewModel: NoteViewModel ) {
       this.noteViewModel=noteViewModel
       this.mBeanList = mBeanList;
       this.mContext = context;
       //查找xml里面的布局文件并实例化
       mLayoutInflater = LayoutInflater.from(mContext)
   }

    fun setViewType(viewType: Int) {
        this.viewType = viewType
    }
    override fun getItemViewType(position: Int): Int {
        return viewType
    }

    //不必每次都创建对象
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_LINEAR_LAYOUT) {
            val view =
                mLayoutInflater.inflate(R.layout.list_item_layout, parent, false)
            MyViewHolder(view)
        } else  {
            val view =
                mLayoutInflater.inflate(R.layout.list_item_grid_layout, parent, false)
            MyGridViewHolder(view)
        }
    }
    //数据的绑定
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder == null) {
            return
        }
        //判断判断
        if (holder is MyViewHolder) {
            bindMyViewHolder(holder as MyViewHolder, position)
        } else if (holder is MyGridViewHolder) {
            bindGridHolder(holder as MyGridViewHolder, position)
        }
    }
    private fun bindMyViewHolder(holder: MyViewHolder, position: Int) {
        val note = mBeanList[position] as Note
        holder.mTvTitle.text = note.title
        holder.mTvContent.text = note.content
        holder.mTvTime.text = note.createTime
        holder.rlContainer.setOnClickListener {
            val intent = Intent(mContext, EditActivity::class.java)
            intent.putExtra("note", note)
            mContext.startActivity(intent)
        }
        holder.rlContainer.setOnLongClickListener { // 弹出弹窗
            val dialog =
                Dialog(mContext, android.R.style.ThemeOverlay_Material_Dialog_Alert)
            val dialogView: View =
                mLayoutInflater.inflate(R.layout.list_item_dialog_layout, null)
            val tvDelete = dialogView.findViewById<TextView>(R.id.tv_delete)
            val tvEdit = dialogView.findViewById<TextView>(R.id.tv_edit)
            val tvZd = dialogView.findViewById<TextView>(R.id.tv_zhiDing)
            tvDelete.setOnClickListener {
                noteViewModel.delete(note)
                dialog.dismiss()
            }

            tvZd.setOnClickListener {
//                val s: Note = mBeanList.removeAt(position)
//                mBeanList.add(0, s)
//                notifyDataSetChanged()
//                dialog.dismiss()
            }


//跳转到编辑页面
            tvEdit.setOnClickListener {
                val intent = Intent(mContext, EditActivity::class.java)
//                获取,activity间信息传递
                intent.putExtra("note", note)
                mContext.startActivity(intent)
                dialog.dismiss()
            }
            dialog.setContentView(dialogView)
            dialog.setCanceledOnTouchOutside(true)
            dialog.show()
            true
        }
    }
    private fun bindGridHolder(holder: MyGridViewHolder, position: Int) {
        val note = mBeanList[position] as Note
        holder.mTvTitle.text = note.title
        holder.mTvContent.text = note.content
        holder.mTvTime.text = note.createTime
        holder.rlContainer.setOnClickListener {
            val intent = Intent(mContext, EditActivity::class.java)
            intent.putExtra("note", note)
            mContext.startActivity(intent)
        }
        holder.rlContainer.setOnLongClickListener { // 弹出弹窗
            val dialog =
                Dialog(mContext, android.R.style.ThemeOverlay_Material_Dialog_Alert)
            val dialogView: View =
                mLayoutInflater.inflate(R.layout.list_item_dialog_layout, null)
            val tvDelete = dialogView.findViewById<TextView>(R.id.tv_delete)
            val tvEdit = dialogView.findViewById<TextView>(R.id.tv_edit)
            val tvZd = dialogView.findViewById<TextView>(R.id.tv_zhiDing)
            tvDelete.setOnClickListener {
                noteViewModel.delete(note)
                dialog.dismiss()
            }

            tvZd.setOnClickListener {
//                val s: Note = mBeanList.removeAt(position)
//                mBeanList.add(0, s)
//                notifyDataSetChanged()
//                dialog.dismiss()
            }


//跳转到编辑页面
            tvEdit.setOnClickListener {
                val intent = Intent(mContext, EditActivity::class.java)
//                获取,activity间信息传递
                intent.putExtra("note", note)
                mContext.startActivity(intent)
                dialog.dismiss()
            }
            dialog.setContentView(dialogView)
            dialog.setCanceledOnTouchOutside(true)
            dialog.show()
            true
        }
    }



    override fun getItemCount(): Int {
        return mBeanList.size
    }
    internal class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mTvTitle: TextView
        var mTvContent: TextView
        var mTvTime: TextView
        var rlContainer: ViewGroup

        init {
            mTvTitle = itemView.findViewById(R.id.tv_title)
            mTvContent = itemView.findViewById(R.id.tv_content)
            mTvTime = itemView.findViewById(R.id.tv_time)
            rlContainer = itemView.findViewById(R.id.rl_item_container)
        }
    }
    internal class MyGridViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mTvTitle: TextView
        var mTvContent: TextView
        var mTvTime: TextView
        var rlContainer: ViewGroup

        init {
            mTvTitle = itemView.findViewById(R.id.tv_title)
            mTvContent = itemView.findViewById(R.id.tv_content)
            mTvTime = itemView.findViewById(R.id.tv_time)
            rlContainer = itemView.findViewById(R.id.rl_item_container)
        }
    }
}


